]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
util: Implement fixMarkup in util as opposed to messageList
authorSam Spilsbury <sam@endlessm.com>
Tue, 13 Jun 2017 16:50:03 +0000 (00:50 +0800)
committerMarge Bot <marge-bot@gnome.org>
Tue, 21 May 2024 13:42:28 +0000 (13:42 +0000)
We want to be able to import it in the markup unit test without
bringing in a UI dependency.

https://bugzilla.gnome.org/show_bug.cgi?id=783738

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3164>

js/misc/util.js
js/ui/messageList.js
tests/unit/markup.js

index 161733b5d2e1b105797659488633ff232f902008..4c172988afb0a449f74fd7bdc066532b5e964f30 100644 (file)
@@ -2,6 +2,7 @@
 
 import Gio from 'gi://Gio';
 import GLib from 'gi://GLib';
+import Pango from 'gi://Pango';
 import Shell from 'gi://Shell';
 import St from 'gi://St';
 import GnomeDesktop from 'gi://GnomeDesktop';
@@ -189,6 +190,35 @@ function _handleSpawnError(command, err) {
         ({notifyError}) => notifyError(title, err.message));
 }
 
+/**
+ * Fix up embedded markup so that it can be displayed correctly in
+ * UI elements such as the message list. In some cases, we might want to
+ * keep some of the embedded markup, so specify allowMarkup for that case
+ *
+ * @param {string} text containing markup to escape and parse
+ * @param {boolean} allowMarkup to allow embedded markup or just escape it all
+ * @returns the escaped string
+ */
+export function fixMarkup(text, allowMarkup) {
+    if (allowMarkup) {
+        // Support &amp;, &quot;, &apos;, &lt; and &gt;, escape all other
+        // occurrences of '&'.
+        let _text = text.replace(/&(?!amp;|quot;|apos;|lt;|gt;)/g, '&amp;');
+
+        // Support <b>, <i>, and <u>, escape anything else
+        // so it displays as raw markup.
+        _text = _text.replace(/<(?!\/?[biu]>)/g, '&lt;');
+
+        try {
+            Pango.parse_markup(_text, -1, '');
+            return _text;
+        } catch (e) {}
+    }
+
+    // !allowMarkup, or invalid markup
+    return GLib.markup_escape_text(text, -1);
+}
+
 /**
  * Returns an {@link St.Label} with the date passed formatted
  * using {@link formatTime}
index c3039e0202b8e88423deaad01501d8b2fb66779a..ddd5abb17ff225b88c20581bdb77e267d9776880 100644 (file)
@@ -18,32 +18,6 @@ const MESSAGE_ANIMATION_TIME = 100;
 
 const DEFAULT_EXPAND_LINES = 6;
 
-/**
- * @param {string} text
- * @param {boolean} allowMarkup
- * @returns {string}
- */
-export function _fixMarkup(text, allowMarkup) {
-    if (allowMarkup) {
-        // Support &amp;, &quot;, &apos;, &lt; and &gt;, escape all other
-        // occurrences of '&'.
-        let _text = text.replace(/&(?!amp;|quot;|apos;|lt;|gt;)/g, '&amp;');
-
-        // Support <b>, <i>, and <u>, escape anything else
-        // so it displays as raw markup.
-        // Ref: https://developer.gnome.org/notification-spec/#markup
-        _text = _text.replace(/<(?!\/?[biu]>)/g, '&lt;');
-
-        try {
-            Pango.parse_markup(_text, -1, '');
-            return _text;
-        } catch (e) {}
-    }
-
-    // !allowMarkup, or invalid markup
-    return GLib.markup_escape_text(text, -1);
-}
-
 export const URLHighlighter = GObject.registerClass(
 class URLHighlighter extends St.Label {
     _init(text = '', lineWrap, allowMarkup) {
@@ -127,7 +101,7 @@ class URLHighlighter extends St.Label {
     }
 
     setMarkup(text, allowMarkup) {
-        text = text ? _fixMarkup(text, allowMarkup) : '';
+        text = text ? Util.fixMarkup(text, allowMarkup) : '';
         this._text = text;
 
         this.clutter_text.set_markup(text);
@@ -560,7 +534,7 @@ export const Message = GObject.registerClass({
 
     set title(text) {
         this._titleText = text;
-        const title = text ? _fixMarkup(text.replace(/\n/g, ' '), false) : '';
+        const title = text ? Util.fixMarkup(text.replace(/\n/g, ' '), false) : '';
         this.titleLabel.clutter_text.set_markup(title);
         this.notify('title');
     }
index 885b213a2fffe63bc375c43934188fd64eca36b6..78c120976bb68008f17353225d14ea40a099307a 100644 (file)
@@ -5,10 +5,7 @@
 const JsUnit = imports.jsUnit;
 import Pango from 'gi://Pango';
 
-import 'resource:///org/gnome/shell/ui/environment.js';
-import 'resource:///org/gnome/shell/ui/main.js';
-
-import * as MessageList from 'resource:///org/gnome/shell/ui/messageList.js';
+import {fixMarkup} from 'resource:///org/gnome/shell/misc/util.js';
 
 /**
  * Assert that `input`, assumed to be markup, gets "fixed" to `output`,
@@ -21,7 +18,7 @@ import * as MessageList from 'resource:///org/gnome/shell/ui/messageList.js';
 function assertConverts(input, output) {
     if (!output)
         output = input;
-    let fixed = MessageList._fixMarkup(input, true);
+    let fixed = fixMarkup(input, true);
     JsUnit.assertEquals(output, fixed);
 
     let parsed = false;
@@ -41,7 +38,7 @@ function assertConverts(input, output) {
  * @param {string} output the output
  */
 function assertEscapes(input, output) {
-    let fixed = MessageList._fixMarkup(input, false);
+    let fixed = fixMarkup(input, false);
     JsUnit.assertEquals(output, fixed);
 
     let parsed = false;